package com.qf.main.pharmacy.controller;

import com.github.pagehelper.PageInfo;
import com.qf.common.base.result.ResponseResult;
import com.qf.main.pharmacy.common.qo.PharmacyQO;
import com.qf.main.pharmacy.common.vo.PharmacyInventoryVo;
import com.qf.main.pharmacy.common.vo.PharmacyVO;
import com.qf.main.pharmacy.service.PharmacyService;
// import org.apache.shiro.authz.annotation.RequiresRoles;
// import org.springframework.stereotype.Controller;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author chengqiang
 * @Create 2024-02-04-13:26
 * @Description:
 */
@RestController
@RequestMapping("/pharmacy")
@CrossOrigin
@Api(tags = "药品管理")
public class PharmacyController {
    @Resource
    private PharmacyService pharmacyService;


    @GetMapping("/list")
    public ResponseResult<PageInfo<PharmacyVO>> listPharmacy(@RequestParam(value = "pageNum" ,required = false) Integer pageNum,
                                                             @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                             @RequestParam(value = "pharmacyName",required = false)String pharmacyName,
                                                             @RequestParam(value = "priceSort",required = false)Integer priceSort,
                                                             @RequestParam(value = "dateSort",required = false)Integer dateSort,
                                                             @RequestParam(value = "types",required = false)Integer types,
                                                             @RequestParam(value = "delStatus",required = false)Integer delStatus){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        return ResponseResult.success(pharmacyService.listPharmacy(pageNum,pageSize,pharmacyName,priceSort,dateSort,types,delStatus));
    }
    @GetMapping("/list/types")
    public ResponseResult<PageInfo<PharmacyVO>> listPharmacyTypes(@RequestParam(value = "pageNum" ,required = false) Integer pageNum,
                                                             @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                             @RequestParam(value = "types",required = false)Integer types,
                                                                  @RequestParam(value = "delStatus",required = false)Integer delStatus){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=4;
        }
        return ResponseResult.success(pharmacyService.listTypesPharmacy(pageNum,pageSize,types,delStatus));
    }
    @GetMapping("/list/examine")
    public ResponseResult<PageInfo<PharmacyVO>> listPharmacyExamine(@RequestParam(value = "pageNum" ,required = false) Integer pageNum,
                                                             @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                             @RequestParam(value = "examineStatus",required = false)Integer examineStatus ){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        return ResponseResult.success(pharmacyService.listPharmacyByExamine(pageNum,pageSize,examineStatus));
    }
    @GetMapping("/get")
    public ResponseResult<PharmacyVO> getPharmacy(@RequestParam(value = "id" ,required = false) Integer id){

        return ResponseResult.success(pharmacyService.getPharmacy(id));
    }


    @PostMapping("/save")
    public ResponseResult<Integer> savePharmacy(@RequestBody PharmacyQO pharmacyQO){
        return ResponseResult.success(pharmacyService.savePharmacy(pharmacyQO));
    }
    @PostMapping("/save/upload")
    public ResponseResult<String> savePharmacyImag(@RequestParam("file") MultipartFile file){
        return ResponseResult.success(pharmacyService.uploadImage(file));
    }
    @PostMapping("/edit")
    public ResponseResult<Integer> editPharmacy(@RequestBody PharmacyQO pharmacyQO){
        return ResponseResult.success(pharmacyService.modifyPharmacy(pharmacyQO));
    }
    @DeleteMapping("/erase")
    public ResponseResult<Integer> erasePharmacy(Integer id){

        return ResponseResult.success(pharmacyService.removePharmacy(id));
    }
    @DeleteMapping("/examine")
    // @RequiresRoles(value = {"ROLE_ADMIN"})
    public ResponseResult<Integer> examinePharmacy(@RequestParam(value = "id")Integer id,@RequestParam(value = "examineStatus" )Integer examineStatus){
        PharmacyQO pharmacyQO = new PharmacyQO();
        pharmacyQO.setPharmacyId(id);
        pharmacyQO.setExamineStatus(examineStatus);
        pharmacyQO.setDelStatus(1);
        return ResponseResult.success(pharmacyService.modifyPharmacy(pharmacyQO));
    }
    @DeleteMapping("/del")
    public ResponseResult<Integer> delPharmacy(@RequestParam(value = "id")Integer id,@RequestParam(value = "delStatus" )Integer delStatus,
                                               @RequestParam(value = "examineStatus" ) Integer examineStatus){
        PharmacyQO pharmacyQO = new PharmacyQO();
        pharmacyQO.setPharmacyId(id);
        pharmacyQO.setDelStatus(delStatus);
        pharmacyQO.setExamineStatus(examineStatus);
        return ResponseResult.success(pharmacyService.modifyPharmacy(pharmacyQO));
    }


    @GetMapping("/inventory/list")
    public ResponseResult<PageInfo<PharmacyInventoryVo>> listInventory(@RequestParam(value = "pageNum" ,required = false) Integer pageNum,
                                                                       @RequestParam(value = "pageSize" ,required = false) Integer pageSize,
                                                                       @RequestParam(value = "pharmacyId",required = false)Integer pharmacyId){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }
        return ResponseResult.success(pharmacyService.listInventory(pageNum,pageSize,pharmacyId));
    }


    @GetMapping("/inventory/reduce")
    public ResponseResult<Integer> reduceInventory(@RequestParam(value = "orderMallId" ,required = false) Integer orderMallId,
                                                                       @RequestParam(value = "orderMallNumber" ,required = false) Integer orderMallNumber){

        return ResponseResult.success(pharmacyService.reduceInventory(orderMallId,orderMallNumber));
    }
}
